<template >
	<el-container class="home-container">
		<el-header>
			<div>
				<img src="../assets/heima.png" alt />
				<span>电商后台管理系统</span>
			</div>
			<el-button type="info" @click="logout">退出</el-button>
		</el-header>
		<el-container>
			<el-aside :width="isCollapse?'64px':'200px'">
				<div class="toggle-button" @click="toggleCollapse">|||</div>
				<!-- menu -->
				<el-menu
					background-color="#323642"
					text-color="#fff"
					active-text-color="#9cdcfe"
					unique-opened
					:collapse="isCollapse"
					:collapse-transition="false"
					router
					:default-active="activePath"
				>
					<!-- 一级菜单 -->
					<el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
						<template slot="title">
							<i :class="iconsObj[item.id]"></i>
							<span>{{item.authName}}</span>
						</template>

						<!-- 二级菜单 -->
						<el-menu-item
							:index="'/'+subitem.path"
							v-for="subitem in item.children"
							:key="subitem.id"
							@click="saveNavState('/'+subitem.path)"
						>
							<template slot="title">
								<i class="el-icon-menu"></i>
								<span>{{subitem.authName}}</span>
							</template>
						</el-menu-item>
					</el-submenu>
				</el-menu>
			</el-aside>
			<el-main>
				<!-- 子路由占位符 -->
				<router-view></router-view>
			</el-main>
		</el-container>
	</el-container>
</template>

<script>
	export default {
		data() {
			return {
				menuList: [],
				iconsObj: {
					"125": "iconfont icon-user",
					"103": "iconfont icon-tijikongjian",
					"101": "iconfont icon-shangpin",
					"102": "iconfont icon-danju",
					"145": "iconfont icon-baobiao"
				},
				isCollapse: false,
				// 记录高亮的菜单项
				activePath: ""
			};
		},
		watch: {
			//  监听路由,路由发生变化时候,改变二级菜单高亮
			$route(to, from) {
				// console.log(to.path);
				this.activePath = to.path;
			}
		},
		created() {
			this.getMenuList(); // 获取页面左侧初始化数据
			// 教学视频里的,回退,二级菜单不能及时高亮,所以从sessionStorage数据没用
			// 需要另外想别的办法
			// this.activePath = window.sessionStorage.getItem("path");

			// console.log(location.hash);
			// 设置菜单高亮                // 回退依旧不能显示当前路由所对应的内容
			this.activePath = location.hash.slice(1);
			// console.log("created" + this.activePath);
		},
		methods: {
			logout() {
				window.sessionStorage.clear();
				this.$router.push("/login");
			},
			// 获取左侧菜单数据请求,
			async getMenuList() {
				const { data: res } = await this.$http.get("menus");
				// console.log(res);
				if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
				// console.log(res.data);
				this.menuList = res.data;
			},
			toggleCollapse() {
				this.isCollapse = !this.isCollapse;
			},
			saveNavState(activePath) {
				// 教学视频里的,回退,二级菜单不能及时高亮,所以从sessionStorage数据没用
				// 需要另外想别的办法
				// window.sessionStorage.setItem("path", activePath);
				// this.activePath = activePath;
				// console.log("event:" + this.activePath);
			}
		}
	};
</script>

<style lang="less" scoped>
	.home-container {
		height: 100%;
	}
	.el-header {
		background-color: #373d41;
		display: flex;
		justify-content: space-between;
		padding-left: 0;
		align-items: center;
		color: #fff;
		font-size: 20px;
		> div {
			display: flex;
			align-items: center;
			span {
				margin-left: 15px;
			}
		}
	}

	.el-aside {
		background-color: #333744;
		.el-menu {
			border-right: none;
		}
	}

	.el-main {
		background-color: #eaedf1;
	}

	.iconfont {
		margin-right: 10px;
	}

	.toggle-button {
		background-color: #4a5064;
		font-size: 10px;
		line-height: 24px;
		color: #fff;
		text-align: center;
		letter-spacing: 0.2em;
		cursor: pointer;
	}
</style>